<?php

namespace app\business\controller;
use think\Controller;
use think\Request;
use app\business\model\SaasBusiness;
use app\common\validate\BusinessValidate;

class Business extends Base
{
    public function businesList(Request $request)
    {
        $data['oneMenuName'] = '商家管理';
        $data['twoMenuName'] = '商家列表';

        $search = $request->get('search');
        $map = [];
        $where = [];
        if ($search){
            $where['name|user_name|phone'] = ['LIKE','%'.$search.'%'];
            $map['query']['search'] = $search;
        }

        $lists = SaasBusiness::where($where)->paginate(10, false, $map);
        return $this->fetch('', compact('data', 'lists'));
    }

    public function addBusines(Request $request)
    {
        if ($request->isPost()){

            $data = $request->post();

            $result = $this->validate($data, 'BusinessValidate');
            if($result !== true){
                $ajax['msg'] = $result;
                return json($ajax);
            }

            // 查询用户表中是否有该电话号码
            $is_user = db('user')->where('phone', $data['phone'])->find();
            if ($is_user){
                $data['user_id'] = $is_user['id'];
                $res = SaasBusiness::create($data);
                if ($res->id){
                    $ajax['code'] = 1;
                    $ajax['msg'] = '操作成功';
                    $ajax['url'] = url('/admin/businesList');
                }else{
                    $ajax['msg'] = '操作失败';
                }
            }else{
                $ajax['msg'] = '没有这个手机号';
            }

            return json($ajax);

        }else{
            $data['oneMenuName'] = '商家管理';
            $data['twoMenuName'] = '添加商家';
            return $this->fetch('', compact('data'));
        }
    }

    public function edit(Request $request)
    {
        if ($request->isPost()){
            $data = $request->post();

            $result = $this->validate($data, 'BusinessValidate');
            if($result !== true){
                $ajax['msg'] = $result;
                return json($ajax);
            }

            // 查询用户表中是否有该电话号码
            $is_user = db('user')->where('phone', $data['phone'])->find();
            if ($is_user){
                $res = SaasBusiness::update($data);
                if($res !== false){
                    $ajax['code'] = 1;
                    $ajax['msg'] = '操作成功';
                    $ajax['url'] = url('/admin/businesList');

                }else {
                    $ajax['msg'] = '操作失败';
                }
            }else{
                $ajax['msg'] = '没有这个手机号';
            }
            return json($ajax);

        }else{
            $data['oneMenuName'] = '商家管理';
            $data['twoMenuName'] = '修改商家';

            $id = input('id');
            $res = SaasBusiness::get($id);
            return $this->fetch('add_busines', compact('data', 'id', 'res'));
        }
    }

    public function del(Request $request)
    {
        if ($request->isPost())
        {
            $id = input('id');
            $save['id'] = $id;
            $save['status'] = 0;
            $res = SaasBusiness::update($save);
            if($res !== false){
                $ajax['code'] = 1;
                $ajax['msg'] = '操作成功';
            }else{
                $ajax['msg'] = '操作失败';
            }
            return json($ajax);
        }
    }
}